<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑</title>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/js/jquery.js"></script>
    <script src="/js/myAjax.js"></script>
    <script src="/js/utlis/edit.js"></script>
</head>
<body>
<div style="margin-left: 100px; margin-top: 120px">
    <div type="hidden" id="groupId"></div>
    <div class="layui-form-item">
        <label class="layui-form-label">组名：</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" id="name" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">组长：</label>
        <div class="layui-input-inline">
            <select name="leader" lay-verify="required" id="leader" style="width: 190px;height: 30px">

            </select>
        </div>
        <div id="add-leader"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">成立时间：</label>
        <div class="layui-input-inline"> <!-- 注意：这一层元素并不是必须的 -->
            <input type="text" readonly class="layui-input" id="establishedTime">
        </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="editAdd('/g/group/edit',getData())">立即提交</button>
            <button type="reset" onclick="resetEdit('/html/group/groupEdit.html')" class="layui-btn layui-btn-primary">
                点击重置
            </button>
        </div>
    </div>
</div>
<script>
    //获取页面输入的值，传递给保存的方法editAdd（）；
    function getData() {
        let data = {
            id: sessionStorage.getItem("groupId"),
            name: $("#name").val(),
            leader: $("#leader").find("option:selected").text(),
            establishedTime: $("#establishedTime").val(),
        }
        return data;//必须带返回值
    }

    //点击编辑页面，通过id去后台查询获得数据给编辑页面进行赋值
    let id = sessionStorage.getItem("groupId")
    findById('/g/group/findById', id);

    function findById(url, id) {
        let result = myAjax(url, 'post', {id: id});
        $("#name").val(result.data.name);
        let data = myAjax('/g/group/selectMember', 'get', {id: id});
        console.log(data);
        let html = "";
        if (data.data == null) {
            html += '<option value="0">暂无成员</option>';
        } else {
            html+='<option value="0">请选择组长</option>'
            for (let i = 0; i < data.data.length; i++) {
                html += '<option value="' + data.data[i].id + '">' + data.data[i].nickName + '</option>'
            }
        }
        $("#leader").html(html);
        for (let i=0;i<data.data.length;i++){
            if (result.data.leader == data.data[i].nickName) {
                $('#leader option').filter(function () {
                    return $(this).text() == data.data[i].nickName;
                }).prop("selected", true);
            }
        }
        $("#establishedTime").val(result.data.establishedTime);
    }
</script>
</body>
</html>